package com.authine.cloudpivot.common.extension.customized.kafka;

import com.alibaba.cola.exception.BizException;
import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.common.extension.customized.service.DepartmentSyncService;
import com.yundasys.hrcenter.dto.PcOrganizationInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author saiwen
 * @date 2021/5/23 18:04
 * description 组织数据消费
 */
@Component
@Slf4j
public class OrgConsumer {

    @Autowired
    DepartmentSyncService departmentSyncService;

    @KafkaListener(topics = {"HR_ORG_TOPIC"})
    public void listen(ConsumerRecord<String, String> consumer){
        Optional<String> kafkaMsg = Optional.ofNullable(consumer.value());
        if (kafkaMsg.isPresent()){
            String organizationInfoStr = kafkaMsg.get();
            log.info("kafka增量组织/部门数据为：{}", organizationInfoStr);
            PcOrganizationInfoDTO organizationInfoDTO = JSON.parseObject(organizationInfoStr, PcOrganizationInfoDTO.class);
            try {
                departmentSyncService.syncData(organizationInfoDTO);
            } catch (BizException e) {
                log.error("组织同步-业务异常，error is :", e);
            }
        }
    }

}
